﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
using Haozes.FxClient.Sip;
using Haozes.FxClient.Core;
using Haozes.FxClient.CommUtil;
using System.Xml;

namespace Haozes.FxClient
{
    public class MessageParser
    {
        private ConversationMgr convMgr;

        public MessageParser(ConversationMgr convMgr)
        {
            this.convMgr = convMgr;
        }

        public void ReceiveSipMessage(object sender, ConversationArgs e)
        {
            SipMessage packet = e.RawPacket;
            DebugWriter.WriteRCVPacket(packet);
            SipRequest req = packet as SipRequest;
            if (req != null)
            { //这是种很怪异的情况,添加好友后,返回一个callID居然不是一个数值
                string sipMethod = req.Method;
                int callID = -1;
                bool canParse = int.TryParse(req.CallID.Value, out callID);
                if (sipMethod == SipMethodName.Benotify && !canParse)
                {
                    DebugWriter.WriteLine(string.Format("CallID居然不是数值类型{0}", callID));
                    return;
                }
            }

            Conversation conv = this.convMgr.Find(packet);
            if (conv != null)
            {
                conv.RcvPacket(packet);
            }

            if (req != null)
            {
                string sipMethod = req.Method;
                int callID = int.Parse(req.CallID.Value);
                switch (sipMethod)
                {
                    case SipMethodName.Invite:
                        Conversation newConv = this.convMgr.Create(req);
                        if (newConv != null)
                        {
                            newConv.MsgRcv += new EventHandler<ConversationArgs>(this.convMgr.RaiseMsgRcv);
                            newConv.RcvPacket(req);
                        }
                        break;
                    case SipMethodName.Message:
                        if (req.ContentType.Value == "text/plain")
                        { //短信
                            if (this.convMgr.Find(req) == null)
                            {
                                Conversation newSMSConv = this.convMgr.Create(req);
                                if (newSMSConv != null)
                                {
                                    newSMSConv.MsgRcv += new EventHandler<ConversationArgs>(this.convMgr.RaiseMsgRcv);
                                    newSMSConv.RcvPacket(req);
                                }
                            }
                        }
                        break;
                    case SipMethodName.Bye:
                        this.convMgr.Remove(callID);
                        break;
                    case SipMethodName.Benotify:
                        if (string.Equals(req.Event.Value, SipEvent.registration.ToString(), StringComparison.OrdinalIgnoreCase))
                        { //registration
                            if (req.Body.IndexOf("deregistered") > 0)
                            {
                                this.convMgr.RaiseDeregistered(this, null);
                            }
                        }
                        else if (string.Equals(req.Event.Value, SipEvent.contact.ToString(), StringComparison.OrdinalIgnoreCase))
                        { //contact
                            XmlDocument document = new XmlDocument();
                            document.LoadXml(e.Text);
                            XmlNode node = document.SelectSingleNode("events/event");
                            if (node != null)
                            {
                                string eventType = XmlHelper.ReadXmlAttributeString(node, "type");
                                if (eventType == "UpdateBuddy")
                                {
                                    convMgr.RaiseUpdateBuddy(this, e);
                                }
                                else if (eventType == "AddBuddyApplication")
                                {
                                    convMgr.RaiseAddBuddyApplication(this, new ConversationArgs(IMType.AddBuddyRequest, req));
                                }
                            }
                        }
                        break;
                    default:
                        break;
                } //switch end
            }
        }
    }
}
